using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FishNet.Object;
using UnityEngine.UI;
using FishNet.Managing.Timing;
using FishNet.Component.ColliderRollback;
using System;
using FishNet.Connection;

public class PlayerControllerProjectiles : NetworkBehaviour
{
    private bool IsPlayer1;
    private Text _playerText;
    private Text _messageText;

    public float Speed = 2;

    public GameObject BulletPrefab;

    private const float MAX_PASSED_TIME = 1.3f;

    // Start is called before the first frame update
    private void Start()
    {
        if (transform.position.z < 0)
        {
            IsPlayer1 = true;
        }

        if (IsPlayer1 == false)
        {
            transform.position = new Vector3(10, transform.position.y, transform.position.z);
        }

        GameObject go = GameObject.Find("PlayerText");
        _playerText = go.GetComponent<Text>();

        GameObject go_message = GameObject.Find("MessageText");
        _messageText = go_message.GetComponent<Text>();
    }

    // Update is called once per frame
    private void Update()
    {
        if (IsOwner)
        {
            if (IsPlayer1 == true)
            {
                _playerText.text = "Player 1";

                if (Input.GetKeyDown(KeyCode.Space))
                {
                    ClientFire();

                    //RpcSpawnBullet(transform.position);
                }
            }
            else
            {
                _playerText.text = "Player 2";

                transform.Translate(Vector3.left * Speed * Time.deltaTime);

                if (transform.position.x < -10)
                {
                    transform.position = new Vector3(10, transform.position.y, transform.position.z);
                }
            }
        }
    }

    //[ServerRpc]
    //private void RpcSpawnBullet(Vector3 position, NetworkConnection conn = null)
    //{
    //    GameObject go_bullet = Instantiate(BulletPrefab, position, Quaternion.identity);
    //    ServerManager.Spawn(go_bullet, conn);
    //}

    [Client]
    private void ClientFire()
    {
        Vector3 firePoint = transform.position;
        Vector3 direction = transform.forward;

        _messageText.text += "Fire Tick=" + base.TimeManager.Tick.ToString() + "\n";

        SpawnBullet(firePoint, direction, 0);
        ServerFire(firePoint, direction, base.TimeManager.Tick);
    }

    [ServerRpc]
    private void ServerFire(Vector3 firePoint, Vector3 direction, uint clientFireTick)
    {
        float passedTime = (float)base.TimeManager.TimePassed(clientFireTick, false);
        passedTime = Mathf.Min(MAX_PASSED_TIME / 2f, passedTime);

        _messageText.text += "Fire Tick=" + base.TimeManager.Tick.ToString() + "\n";

        SpawnBullet(firePoint, direction, passedTime);
        ObserversFire(firePoint, direction, clientFireTick);
    }

    [ObserversRpc(ExcludeOwner = true)]
    public void ObserversFire(Vector3 firePoint, Vector3 direction, uint clientFireTick)
    {
        float passedTime = (float)base.TimeManager.TimePassed(clientFireTick, false);
        passedTime = Mathf.Min(MAX_PASSED_TIME, passedTime);

        _messageText.text += "Fire Tick=" + base.TimeManager.Tick.ToString() + "\n";

        SpawnBullet(firePoint, direction, passedTime);
    }

    private void SpawnBullet(Vector3 firePoint, Vector3 direction, float passedTime)
    {
        GameObject go_bullet = Instantiate(BulletPrefab, firePoint, Quaternion.identity);

        BulletController _bulletController = go_bullet.GetComponent<BulletController>();
        _bulletController.Initialize(direction, passedTime);
    }
}